import { defineStore } from 'pinia'
import {
  getCategory1Api,
  getCategory2Api,
  getCategory3Api,
} from '@/api/product/attr'
import { CategoryResponseData } from '@/api/product/attr/type'
import { CategoryState } from './types/types'

let useCategoryStore = defineStore('Category', {
  state: (): CategoryState => {
    return {
      category1List: [],
      category2List: [],
      category3List: [],
      category1Id: '',
      category2Id: '',
      category3Id: '',
    }
  },
  actions: {
    async getCategory1List() {
      const res: CategoryResponseData = await getCategory1Api()
      if (res.code == 200) {
        this.category1List = res.data
      }
    },
    async getCategory2List() {
      const res: CategoryResponseData = await getCategory2Api(this.category1Id)
      if (res.code == 200) {
        this.category2List = res.data
      }
    },
    async getCategory3List() {
      const res: CategoryResponseData = await getCategory3Api(this.category2Id)
      if (res.code == 200) {
        this.category3List = res.data
      }
    },
  },
  getters: {},
})

export default useCategoryStore
